package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.vo.UcenterMemberVo;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.Vo.LoginVo;
import com.atguigu.educenter.entity.Vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author shy
 * @since 2021-06-16
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
@Api(description = "登录和注册")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;

    // 登陆
    @ApiOperation("登录方法")
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        String token = ucenterMemberService.login(loginVo);
        return R.ok().data("token", token);
    }

    // 注册
    @ApiOperation("注册方法")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation("返回用户信息")
    @GetMapping("/getUserInfoForJwt")
    public R getUserInfoForJwt(HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = ucenterMemberService.getById(id);
        return R.ok().data("userInfo", member);
    }

    //根据用户id查询用户信息
    @PostMapping("/getMemberInfoById/{memberId}")
    public UcenterMemberVo getMemberInfoById(@PathVariable String memberId){
        UcenterMember ucenterMember = ucenterMemberService.getById(memberId);
        UcenterMemberVo ucenterMemberVo = new UcenterMemberVo();
        BeanUtils.copyProperties(ucenterMember, ucenterMemberVo);
        return ucenterMemberVo;
    }

    //根据日期，获取那天注册人数
    @GetMapping("/countRegister/{day}")
    public R countRegister(@PathVariable String day){
        int count = ucenterMemberService.getCountRegister(day);
        return R.ok().data("countRegister", count);
    }

}
